/**
 * 
 */
package vn.org.hcmut.or.app;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import vn.org.hcmut.or.util.Util;

/**
 * @author HUNGUYEN
 * @version 1.0
 *
 * Dac ta: day la file tao tu dong du lieu dau vao gom numOfJob, minsplit va strInF2Path
 * trong do:
 * 		- numOfJob   =  [lbNumOfJob,ubNumOfJob] = [10,50] // so cong viec can duoc lap lich
 * 		- minSplit   =  [lbMinSplit,ubMinSplit] = [1,4] // gia tri nguong cho mot manh/ mot cong viec 
 * 															duoc thuc hien trong khung 
 * 		- proTimeJob =  [lbProTimeJob,ubProTimeJob] = [1,20]//thoi gian xu ly mot cong viec
 * 		- numOfBreak =  Max(2*minSplit, numOfJob - minSplit)// so thoi diem break thuc hien 
 * 													<--- cai nay chi la de xuat 
 * 		- strInF2Path: duong dan tuong doi cua ten file input
 */
public class MakeInput {

	/**
	 * @param args
	 */
	final int lbNumOfJob = 10;
	final int ubNumOfJob = 50;
	final int lbProTimeJob = 1;
	final int ubProTimeJob = 20;
	final int lbMinSplit = 1;
	final int ubMinSplit = 4;
	
	private int numOfJob;
	private int minSplit;
	private ArrayList<Integer> listProTimeJob;
	private ArrayList<Integer> listPointBreak;
	
	public int generalRandom(int lowerBound,int upperBound){
		Random random = new Random();
		return (random.nextInt(upperBound-lowerBound)+lowerBound);
	}
	
	public MakeInput(){
		this.minSplit = generalRandom(lbMinSplit,ubMinSplit);
		this.numOfJob = generalRandom(lbNumOfJob,ubNumOfJob);
		listProTimeJob = new ArrayList<Integer>();
		listPointBreak = new ArrayList<Integer>();
	}
	
	public MakeInput(int numOfJob, int minsplit){
		this.minSplit = minsplit;
		this.numOfJob = numOfJob;
		listProTimeJob = new ArrayList<Integer>();
		listPointBreak = new ArrayList<Integer>();
	}
	
	private void createJob(){
		for (int index = 0; index <numOfJob;index++){
			int value = Math.max(minSplit, generalRandom(lbProTimeJob,ubProTimeJob));
			listProTimeJob.add(value);
		}
	}
	
	private void createTimeBreak(){
		int numOfBreak = Math.max(numOfJob - minSplit, 2*minSplit);
		int curStart = 0;
		
		for (int index = 0; index <numOfBreak;index++){
			curStart+=generalRandom(minSplit,ubProTimeJob);
			this.listPointBreak.add(curStart);
		}
	}
	
	private String strArrayListInteger(ArrayList<Integer> listInteger){
		String strList = listInteger.get(0).toString();
		for (int index = 1;index < listInteger.size();index++){
			strList+=Util.SPACE+listInteger.get(index).toString();
		}
		return strList;
	}
	
	public void makeFileCustom(String strFilePath){
		try {
			createJob();
			createTimeBreak();
			BufferedWriter out = new BufferedWriter(new FileWriter(strFilePath));
			out.write(numOfJob+Util.SPACE+minSplit);
			out.write(Util.ENDLINE+strArrayListInteger(listProTimeJob));
			out.write(Util.ENDLINE+strArrayListInteger(listPointBreak));
			if (Util.isEndFile) out.write(Util.ENDLINE);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void makeFileDefault(){
		makeFileCustom(Util.strInF2Path);
	}
	/**
	 * @return the numOfJob
	 */
	public int getNumOfJob() {
		return numOfJob;
	}

	/**
	 * @return the minsplit
	 */
	public int getMinsplit() {
		return minSplit;
	}

	/**
	 * @param minsplit the minsplit to set
	 */
	public void setMinsplit(int minsplit) {
		this.minSplit = minsplit;
	}

	/**
	 * @return the listProTimeJob
	 */
	public ArrayList<Integer> getListProTimeJob() {
		return listProTimeJob;
	}

	/**
	 * @param listProTimeJob the listProTimeJob to set
	 */
	public void setListProTimeJob(ArrayList<Integer> listProTimeJob) {
		this.listProTimeJob = listProTimeJob;
	}

	/**
	 * @return the listPointBreak
	 */
	public ArrayList<Integer> getListPointBreak() {
		return listPointBreak;
	}

	/**
	 * @param listPointBreak the listPointBreak to set
	 */
	public void setListPointBreak(ArrayList<Integer> listPointBreak) {
		this.listPointBreak = listPointBreak;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MakeInput inputCustom = new MakeInput(7,2);//tao input voi numOfJob va minSplit theo y muon
		inputCustom.makeFileCustom(".\\input\\input1.txt");//tao file input va duong dan file theo y muon 
		MakeInput inputDefault = new MakeInput();//tao input mac dinh voi numOfJob va minSplit theo ngau nhien
		inputDefault.makeFileDefault();//tao file input va duong dan file theo mac dinh cho san
		
	}

	
}
